package com.seven.thread;

import org.slf4j.LoggerFactory;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author sevenDay
 * @classname SevenThreadFactory
 * @description: 创建自定义的线程池的工厂
 * @date 2017/11/16
 */
public class SevenThreadFactory implements ThreadFactory{

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(SevenThreadFactory.class);


    private static final AtomicInteger poolNumber = new AtomicInteger(1);
    private final ThreadGroup group;
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final String namePrefix;

    SevenThreadFactory() {
        SecurityManager s = System.getSecurityManager();
        group = (s != null) ? s.getThreadGroup() :
                Thread.currentThread().getThreadGroup();
        namePrefix = "[seven-线程]  pool-" + poolNumber.getAndIncrement() +"-thread-";
    }

    /**
     * 设置线程的初始化状态 信息，分为三种不同的线程创建方式进行设置有效的信息
     * @param r
     * @return
     */
    @Override
    public Thread newThread(Runnable r) {
        String threadName = "";
        //namePrefix + threadNumber.getAndIncrement()
        Thread t = new Thread(group, r,threadName
                ,
                0);
        if (t.isDaemon()) {
            t.setDaemon(false);
        }
        if (t.getPriority() != Thread.NORM_PRIORITY) {
            //设置优先级
            t.setPriority(Thread.NORM_PRIORITY);
        }
        return t;
    }
}
